import javax.swing.tree.TreeNode;
import java.util.*;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-10-18
 * Time: 0:43
 */
public class Demo1 {
    /**注意：
     * 1. wait必须在notify之前运行
     * 2.notify和wait必须针对同一个对象锁才可以生效
     *
     * @param args
     */
    public static void main(String[] args) {
        Object locker = new Object();
        Object locker2 = new Object();

        Thread t1 = new Thread(()->{

            try {
                System.out.println("wait 之前");
                synchronized (locker) {
                    locker.wait();
                }
                System.out.println("wait 之后");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(()->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            Scanner scanner = new Scanner(System.in);
            System.out.println("输入任意内容, 通知唤醒 t1");
            scanner.next();

            synchronized (locker) {
                locker.notify();
            }
        });


        t1.start();
        t2.start();
    }
}
